// pages/customdinner/customdinner.js
var util = require('../../utils/util.js')
var listid;
var dinnerlist;

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    listid = options.id;
    dinnerlist = util.getDinnerList();
    if (listid == -1) {
      listid = dinnerlist.list.length;
      dinnerlist.selected = listid;
      dinnerlist.list.push(JSON.parse('{ "id": ' + dinnerlist.list.length + ', "name": "列表名", "foodlist": ["请添加"] }')); 
    }
    console.log(listid);
    console.log(dinnerlist);
    var foodlist = dinnerlist.list[listid].foodlist;
    var name = dinnerlist.list[listid].name;
    this.setData({ name: name, foodlist: foodlist });
  },

  addFood: function() {
    dinnerlist.list[listid].foodlist.push("请添加");
    var foodlist = dinnerlist.list[listid].foodlist;
    this.setData({ foodlist: foodlist });
  },

  bindTitleInput: function(e) {
    var title = e.detail.value;
    console.log(title);

    dinnerlist.list[listid].name = title;

    var name = dinnerlist.list[listid].name;
    this.setData({ name: name });

  },

  bindItemInput: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(index);
    var value = e.detail.value;
    console.log(value);

    dinnerlist.list[listid].foodlist.splice(index, 1, value);
    
    var foodlist = dinnerlist.list[listid].foodlist;
    this.setData({ foodlist: foodlist });
  },

  deleteFood: function(event) {
    var index = event.currentTarget.dataset.index;
    dinnerlist.list[listid].foodlist.splice(index, 1);

    var foodlist = dinnerlist.list[listid].foodlist;
    this.setData({ foodlist: foodlist });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    util.setDinnerList(dinnerlist);
    console.log(dinnerlist);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})